package com.camel.docker.entity.search;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.ListContainersCmd;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
public class ContainerForm implements Serializable {
    private static final long serialVersionUID = 1L;

    // 状态
    public String state;

    public Boolean isAll;

    public String name;

    public String image;

    public ListContainersCmd buildListCmd(DockerClient dockerClient) {
        ListContainersCmd containersCmd = dockerClient.listContainersCmd().withShowAll(isAll).withNameFilter(CollectionUtils.arrayToList(new String[]{name}));
        if (StringUtils.isNotBlank(image)) {
            List<String> images = new ArrayList();
            images.add(image);
            containersCmd.withAncestorFilter(images);
        }
        return containersCmd;
    }
}
